﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using HslCommunication.Core;
using HslCommunication.Core.Net;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 订阅分类的核心组织对象
	/// </summary>
	// Token: 0x020000FD RID: 253
	public class PushGroupClient : IDisposable
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06001531 RID: 5425 RVA: 0x0006E388 File Offset: 0x0006C588
		public PushGroupClient()
		{
			this.appSessions = new List<AppSession>();
			this.simpleHybird = new SimpleHybirdLock();
		}

		/// <summary>
		/// 新增一个订阅的会话
		/// </summary>
		/// <param name="session">会话</param>
		// Token: 0x06001532 RID: 5426 RVA: 0x0006E3B7 File Offset: 0x0006C5B7
		public void AddPushClient(AppSession session)
		{
			this.simpleHybird.Enter();
			this.appSessions.Add(session);
			this.simpleHybird.Leave();
		}

		/// <summary>
		/// 移除一个订阅的会话
		/// </summary>
		/// <param name="clientID">客户端唯一的ID信息</param>
		// Token: 0x06001533 RID: 5427 RVA: 0x0006E3E0 File Offset: 0x0006C5E0
		public bool RemovePushClient(string clientID)
		{
			bool result = false;
			this.simpleHybird.Enter();
			for (int i = 0; i < this.appSessions.Count; i++)
			{
				bool flag = this.appSessions[i].ClientUniqueID == clientID;
				if (flag)
				{
					Socket workSocket = this.appSessions[i].WorkSocket;
					if (workSocket != null)
					{
						workSocket.Close();
					}
					this.appSessions.RemoveAt(i);
					result = true;
					break;
				}
			}
			this.simpleHybird.Leave();
			return result;
		}

		/// <summary>
		/// 使用固定的发送方法将数据发送出去
		/// </summary>
		/// <param name="content">数据内容</param>
		/// <param name="send">指定的推送方法</param>
		// Token: 0x06001534 RID: 5428 RVA: 0x0006E478 File Offset: 0x0006C678
		public void PushString(string content, Action<AppSession, string> send)
		{
			this.simpleHybird.Enter();
			Interlocked.Increment(ref this.pushTimesCount);
			for (int i = 0; i < this.appSessions.Count; i++)
			{
				send(this.appSessions[i], content);
			}
			this.simpleHybird.Leave();
		}

		/// <summary>
		/// 移除并关闭所有的客户端
		/// </summary>
		// Token: 0x06001535 RID: 5429 RVA: 0x0006E4DC File Offset: 0x0006C6DC
		public int RemoveAllClient()
		{
			this.simpleHybird.Enter();
			for (int i = 0; i < this.appSessions.Count; i++)
			{
				Socket workSocket = this.appSessions[i].WorkSocket;
				if (workSocket != null)
				{
					workSocket.Close();
				}
			}
			int count = this.appSessions.Count;
			this.appSessions.Clear();
			this.simpleHybird.Leave();
			return count;
		}

		/// <summary>
		/// 获取是否推送过数据
		/// </summary>
		/// <returns>True代表有，False代表没有</returns>
		// Token: 0x06001536 RID: 5430 RVA: 0x0006E55C File Offset: 0x0006C75C
		public bool HasPushedContent()
		{
			return this.pushTimesCount > 0L;
		}

		/// <summary>
		/// 释放当前的程序所占用的资源
		/// </summary>
		/// <param name="disposing">是否释放资源</param>
		// Token: 0x06001537 RID: 5431 RVA: 0x0006E578 File Offset: 0x0006C778
		protected virtual void Dispose(bool disposing)
		{
			bool flag = !this.disposedValue;
			if (flag)
			{
				if (disposing)
				{
				}
				this.simpleHybird.Enter();
				this.appSessions.ForEach(delegate(AppSession m)
				{
					Socket workSocket = m.WorkSocket;
					if (workSocket != null)
					{
						workSocket.Close();
					}
				});
				this.appSessions.Clear();
				this.simpleHybird.Leave();
				this.simpleHybird.Dispose();
				this.disposedValue = true;
			}
		}

		/// <summary>
		/// 释放当前的对象所占用的资源
		/// </summary>
		// Token: 0x06001538 RID: 5432 RVA: 0x0006E5FE File Offset: 0x0006C7FE
		public void Dispose()
		{
			this.Dispose(true);
		}

		/// <inheritdoc />
		// Token: 0x06001539 RID: 5433 RVA: 0x0006E609 File Offset: 0x0006C809
		public override string ToString()
		{
			return "PushGroupClient";
		}

		// Token: 0x04000514 RID: 1300
		private List<AppSession> appSessions;

		// Token: 0x04000515 RID: 1301
		private SimpleHybirdLock simpleHybird;

		// Token: 0x04000516 RID: 1302
		private long pushTimesCount = 0L;

		// Token: 0x04000517 RID: 1303
		private bool disposedValue = false;
	}
}
